<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/


namespace Member\Model;
use Think\Model;
use \Org\Util\String;
    
class MemberDialogMessageModel extends MemberModel {

	//protected $insertFields = array('username','usertype','typename','password','powercode','usergroup','groupname','email','unionid','uniontime','regip','dateline','mobile');
	
	//protected $updateFields = array('avatar','SecurityForm');
	
	//字段过滤-----------------------------------------------------------------------------------------------
	//protected $insertFields = array('title','status','rules','icon','color','allowdomainname','allowlogin','allowskillnumber','allowsendsms','feetype','taskfee','SecurityForm');
	//protected $updateFields = array('usertype','typename','password','powercode','usergroup','groupname','email','unionid','uniontime','regip','dateline','mobile','birthday','about','gender','area','phone','qq','provid','cityid','distid','provname','cityname','distname','address','SecurityForm');
	
	
	//字段检查-----------------------------------------------------------------------------------------------
	protected $_validate = array(
		//array('title','require','请输入名称！'),
	);
	
	//数据过滤-----------------------------------------------------------------------------------------------
	function FormatData($array=array()){
		$data['fromuid']=$this->uid;
		$data['toname']=String::msubstr(I('post.username'),0,30,C('S_CHARSET'),false);
		$data['content']=String::msubstr(I('post.content'),0,30,C('S_CHARSET'),false);
		$data['dateline']=$data['lasttime']=NowTime();
		$data['SecurityForm']=I('post.SecurityForm','','string');
		
		return array_merge($array,$data);
	}
	
	function DeleteData(){
		$id=intval($_POST['id']);
		if($id){
			
			$result=$this->where(" id=$id ")->find();
			if($result['id']){
				
				if($result['status']){
					$this->where(" id=$id ")->delete();
				}else{
					$this->where(" id=$id ")->data(array('status'=>$this->uid))->save();
				}
				
				//更新
				$messagecount=$this->where(" aid=$result[aid] AND status!=$this->uid ")->count();
				
				M('MemberDialog')->token(false)->where(" fromuid=$this->uid AND activity=$result[aid] ")->data(array('iscount'=>$messagecount))->save();
				
				return true;
			}

			return false;
			
		}else{
			return false;
		}
	}
	
	function GetChatIm(){
		$aid=intval($_GET['id']);
		if($aid>0){
			$list=$this->where(" fromuid=$this->uid AND aid=$aid OR touid=$this->uid AND aid=$aid ")->order(' dateline DESC ')->limit(50)->select();
			if(!empty($list)){
				for($i=0;$i<count($list);$i++){
					$list[$i]['avatar']=GetAvatarSimple($list[$i]['fromuid'],48,'comment-avatar');
					$list[$i]['dateline']=FormatDate("Y/m/d H:i",$list[$i]['dateline']);
				}
			}
			
			//设置已看
			$model=new \Member\Model\MemberDialogModel();
			$model->UpdateDialogRead(array('list'=>$list),$aid);
			
			return array('list'=>array_reverse($list));
		}else{
			return false;
		}
	}
}

?>